package lime.browser.command;

import lime.browser.view.NavigatorView;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

import com.documentum.fc.common.DfId;

public class FindHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		
		Shell parentShell = HandlerUtil.getActiveShell(event);
		InputDialog dialog = new InputDialog(parentShell, "Find", "Object ID", "", new ObjectIdValidator());
		
		if (dialog.open() == Window.OK) {
			String objectId = dialog.getValue();
			NavigatorView view = (NavigatorView) HandlerUtil.getActivePart(event);
			view.showObjectInTree(objectId);
		}
		
		return null;
	}

	private class ObjectIdValidator implements IInputValidator {

		@Override
		public String isValid(String newText) {
			
			if (StringUtils.isBlank(newText)) {
				return "";
			}
			
			if (!DfId.isObjectId(newText)) {
				return "Not a valid object ID.";
			}
			
			return null;
		}
		
	}
}
